//
//  MoDisplayNode.swift
//  Pods
//
//  Created by zhu xietong on 2017/8/21.
//
//

import UIKit
import AsyncDisplayKit
import Eelay
public protocol MoUITraitEnvironment:class{
    func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
}
open class MoBaseNode: ASDisplayNode,MoUITraitEnvironment{
    open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    }
    
    open func mo_will_display() {
        
    }
    open func mo_end_display() {

    }
        
    
    public override init() {
        super.init()
        __notice.observer(TP.Notice.traitCollectionDidChange,TP.Notice.localizableDidChange) {[weak self] (t, ntype) in
            switch ntype.name.rawValue{
            case TP.Notice.traitCollectionDidChange.name.rawValue:
                self?.traitCollectionDidChange(nil)
            case TP.Notice.localizableDidChange.name.rawValue:
                if let n = self as? MoAtrributeProtocol{
                    n.mo_change_localizable(obj: nil)
                }
            default:
                break
            }
        }

        self.automaticallyManagesSubnodes = true
        initStyle()
        
    }
    
    open func initStyle(){
         
    }
    
    public var __constrainedSize:ASSizeRange = ASSizeRange(min: CGSize(width: Swidth, height: 0.0), max: CGSize(width: Swidth, height: 1000000))
      override open func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
          self.__constrainedSize = constrainedSize
          return fastLayoutSpec(_spec: specGetter())
      }
    
    open func specGetter()->_Spec{
        return .stack([], childs: [])
    }
}

open class MoDisplayNode:MoBaseNode,MoAtrributeProtocol
{
    
    public var nodeIndex:Int = -1
    open func mo_change_localizable(obj: Any?) {
        mo_model_reload()
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    public var opened:Bool{
        get{
            return mo.model["__opened__","0"] == "1"
        }
        set{
            if newValue{
                mo.model["__opened__"] = "1"
            }else{
                mo.model["__opened__"] = "0"
            }
        }
    }
    
   @objc open func defaultClick() {
        if let ctr = mo.controller{
            if let action = mo.model["@click"] as? _Click{
                action.block(ctr)
                return
            }
            if let action = mo.model["@click"] as? _KitClick<Self>{
                if let obj = self as? Self{
                    action.block(obj)
                    return
                }
            }
        }
    }
    
    open func mo_set_controller(controller: UIViewController) {
        
    }
    
    open func mo_model_willSet(new: NSMutableDictionary) {
        
    }
    
    open func mo_model_reload() {
        
    }
    
    open func mo_model_relase(current: NSMutableDictionary) {
        
    }
    
    
    
    open func mo_detail_willSet(new: NSMutableDictionary) {
        
    }
    
    open func mo_detail_reload() {
        
    }
    
    open func mo_detail_relase(current: NSMutableDictionary) {
        
    }
    
    private var _mo = MoAtrribute()
    public var mo:MoAtrribute{
        get{
           
            _mo.ower = self
            return _mo
        }
        set{
            newValue.ower = self
            _mo = newValue
            self.mo_model_willSet(new: newValue.model)
            self.mo_detail_willSet(new: newValue.detail)

            self.mo_model_reload()
        }
    }

    public weak var delegate:EeActionDelegate?
    

    public required override init() {
        super.init()
        willLayoutRules()
        addLayoutRules()
        didLayoutRules()
    }
    open func addLayoutRules()
    {
        //        self.fillPropertyViews()
    }
    
    open func willLayoutRules()
    {
        //        self.fillPropertyViews()
    }
    
    open func didLayoutRules()
    {
        //        self.fillPropertyViews()
    }
    
    
    
    
    open func active(ID actionID:String,object:NSMutableDictionary? = nil) {
        if let obj = object
        {
            self.delegate?.active(onwer: self, actionID: actionID, data: obj)
        }
        else
        {
            self.delegate?.active(onwer: self, actionID: actionID, data: mo.model)
        }
    }
    
    
    
}
